import React, {Component} from 'react';
import {
    StyleSheet,
    Text,
    View,
    Image,
    Dimensions,
    ListView,
    TouchableOpacity,
    TextInput,
    AsyncStorage
} from 'react-native';
import {
    Actions
} from 'react-native-router-flux';
import ButtonComponent from "../../../jscode/common/ButtonComponent";
import Config from '../../../jscode/common/Config'
import Utils from '../../../jscode/common/Utils'
import {Toast,ModalIndicator} from 'teaset';
const STORAGE_KEY_TOKEN = '@AsyncStorageDemo:key_token';
const {height, width} = Dimensions.get('window');
export default class VerificationLoginPage extends Component {
    constructor(props) {
        super(props);
        this.state = {
            phone:'',
            verificationCode:''
        }
    };

    login=()=>{
        let login={
            "phone":this.state.phone,
            "checkCode":this.state.verificationCode,
            "type":"checkCode"
        };
        ModalIndicator.show("正在请求数据......");
        fetch(Config.domain+'/user/login',{
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: Utils.getParamsString(login)
        })
            .then((response) => response.json())
            .then((responseJson) => {
                ModalIndicator.hide();
                if(responseJson.status!==1){
                    AsyncStorage.setItem(STORAGE_KEY_TOKEN,responseJson.data.token);
                    alert(JSON.stringify(responseJson))
                }else{
                    Toast.message(responseJson.msg);
                }
            })
            .catch((error) => {
                ModalIndicator.hide();
                Toast.message(error);
            });
    };

    render() {
        return (
            <View style={styles.container}>
                <View style={styles.topTitle}>
                    <Text style={styles.topTitleMsg}>手机号登录</Text>
                </View>
                <View style={styles.phoneAndPasswordView}>
                    <View style={styles.topTextInputView}>
                        <Text style={styles.TextInputTitleMsg}>手机号</Text>
                        <TextInput
                            ref={ (ref)=>this.topTextInput = ref }
                            autoCapitalize={"none"}
                            defaultValue={this.state.phone}
                            placeholder="请输入手机号"
                            placeholderTextColor='#bababa'
                            autoCorrect={false}
                            style={styles.textInputView}
                            onChangeText={(text)=>{
                                this.setState({
                                    phone:text,
                                })
                            }}/>
                    </View>
                    <View style={styles.bottomTextInputView}>
                        <Text style={styles.TextInputTitleMsg}>验证码</Text>
                        <TextInput
                            ref={ (ref)=>this.bottomTextInput = ref }
                            autoCapitalize={"none"}
                            defaultValue={this.state.verificationCode}
                            placeholder="请输入验证码"
                            placeholderTextColor='#bababa'
                            autoCorrect={false}
                            style={styles.textInputView}
                            onChangeText={(text)=>{
                                this.setState({
                                    verificationCode:text,
                                })
                            }}/>
                        <View style={styles.verificationCodeView}>
                            <ButtonComponent borderColor={{borderColor:'#000000'}}
                                             timerCount = {10}
                                             textColor ={{color:'#000000'}}
                                             onClick = {(shouldStart) =>{
                                                 this._GetVerification(shouldStart);}}/>
                        </View>
                    </View>
                </View>
                <View style={styles.ButView}>
                    <TouchableOpacity onPress={()=>this.goBack()}>
                        <Text style={styles.ButMsg}>用密码登录</Text>
                    </TouchableOpacity>
                    <TouchableOpacity>
                        <Text style={styles.ButMsg}>注册</Text>
                    </TouchableOpacity>
                </View>
                <View style={styles.loginView}>
                    {this.state.phone!==''||this.state.verificationCode!==''
                    ?(<TouchableOpacity
                            onPress={()=>this.login()}
                            style={[styles.loginButView,{backgroundColor:'#3090dd'}]}>
                            <Text style={styles.loginButMsg}>登录</Text>
                        </TouchableOpacity>)
                    :(<TouchableOpacity style={[styles.loginButView,{backgroundColor:'#69a9dd'}]}>
                            <Text style={styles.loginButMsg}>登录</Text>
                        </TouchableOpacity>)}
                </View>
            </View>
        );
    }
    _GetVerification=(shouldStart)=>{
        let login={
            "phone":this.state.phone,
        };
        ModalIndicator.show("正在请求数据......");
        fetch(Config.baseApi+'/user/getCheckCode',{
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: Utils.getParamsString(login)
        })
            .then((response) => response.json())
            .then((responseJson) => {
                ModalIndicator.hide();
                if(responseJson.status!==1){
                    shouldStart(true);
                    Toast.message(JSON.stringify(responseJson))
                }else{
                    Toast.message(responseJson.msg);
                }
            })
            .catch((error) => {
                ModalIndicator.hide();
                Toast.message(error);
            });
    };
    goBack=()=>{
        Actions.pop()
    };
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor:'#ffffff'
    },
    topTitle:{
        marginTop:109,
        marginLeft:30
    },
    topTitleMsg:{
        fontSize:20,
        color:'#000000'
    },
    phoneAndPasswordView:{
        marginTop:24,
        marginLeft:30
    },
    topTextInputView:{
        width:width-60,
        flexDirection:'row',
        alignItems:'center',
        height:40,
        borderColor:'#bababa',
        borderBottomWidth:1
    },
    TextInputTitleMsg:{
        fontSize:15,
        color:'#000000'
    },
    bottomTextInputView:{
        width:width-60,
        flexDirection:'row',
        alignItems:'center',
        height:40,
        borderColor:'#bababa',
        borderBottomWidth:1
    },
    ButView:{
        marginTop:28,
        width:width-60,
        flexDirection:'row',
        justifyContent:'space-between',
        alignItems:'center',
        marginLeft:30
    },
    ButMsg:{
        fontSize:13,
        color:'#3090dd'
    },
    loginView:{
        marginTop:39,
        justifyContent:'center',
        alignItems:'center'
    },
    loginButView:{
        width:width-60,
        alignItems:'center',
        justifyContent:'center',
        borderRadius:5,
        height:44
    },
    loginButMsg:{
        color:'#ffffff',
        textAlign:'center',
        fontSize:17
    },
    textInputView:{
        position: 'absolute',
        left:105,
        height:40,
        width:width-165,
        fontSize:15
    },
    verificationCodeView:{
        position: 'absolute',
        right:0,
        alignItems:'center',
        justifyContent:'center',
        height:40
    }
});
